﻿using AutoMapper;
using Serilog;
using SqlSugar;
using System;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Service.DomianService.Interface;
using WCS_Service.Interface;
using WCS_Utility.ConstString;
using WCS_Utility.Enums;

namespace WCS_Service.DomianService
{
    /// <summary>
    /// WCS主控
    /// </summary>
    public class WCSMainControlManage :IWCSMainControlManage
    {
        protected readonly IAutoEquipmentService _autoEquipmentService;
        protected readonly IAutoEquipmentOperateService _autoEquipmentOperateService;
        protected readonly IComControl _comControl;
        protected readonly ISendMsgToSubService _sendMsgToSubService;
        protected readonly IMsgAbnormalService _msgAbnormalService;
        protected readonly ITaskService _taskService;
        protected readonly IMapPositionService _mapPositionService;
        protected readonly IRCSDomainService _rcsDomainService;

        protected readonly IUpperSysDomainService _upperSysDomainService;
        protected readonly IOpticalDomainService _opticalDomainService;
        protected readonly IAGVDomainService _agvDomainService;
        protected readonly ILineDomainService _lineDomainService;

        protected readonly IMapper _mapper;

        #region 构造
        public WCSMainControlManage(IAutoEquipmentService autoEquipmentService,
                                    IAutoEquipmentOperateService autoEquipmentOperateService,
                                    IComControl comControl,
                                    IMapper mapper,
                                    ISendMsgToSubService sendMsgToSubService,
                                    IOpticalDomainService opticalDomainService,
                                    IAGVDomainService agvDomainService,
                                    IRCSDomainService rcsDomainService,
                                    IMsgAbnormalService msgAbnormalService,
                                    ITaskService taskService,
                                    IUpperSysDomainService upperSysDomainService,
                                    ILineDomainService lineDomainService,
                                    IMapPositionService mapPositionService) 
        {
            _autoEquipmentService = autoEquipmentService;
            _autoEquipmentOperateService = autoEquipmentOperateService;
            _comControl = comControl;
            _sendMsgToSubService = sendMsgToSubService;
            _opticalDomainService = opticalDomainService;
            _agvDomainService = agvDomainService;
            _rcsDomainService = rcsDomainService;
            _mapper = mapper;
            _msgAbnormalService= msgAbnormalService;
            _taskService = taskService;
            _upperSysDomainService = upperSysDomainService;
            _lineDomainService = lineDomainService;
            _mapPositionService = mapPositionService;
        }
        #endregion

        public async Task InitWcsControlService()
        {
            //查询所有可用的自动化设备
            var autoEqInfos = await _autoEquipmentService.QueryAllAutoEquipmentAsync();
            if (autoEqInfos is null || !autoEqInfos.Any())
            {
                Log.Warning($"没有自动化设备信息!");
                return;
            }
            var availableEqs = autoEqInfos.Where(a => a.status == (int)EquipmentState.Available)?.ToList();
            if (availableEqs is null || !autoEqInfos.Any())
            {
                Log.Warning($"没有可用自动化设备信息!");
                return;
            }
            var ids = availableEqs.Where(a=> a.equipmentType != ConstStringData.AGV_eq)?.Select(a => a.id)?.ToList();
            if (ids is null || !ids.Any())
            {
                Log.Warning($"可启动的自动化设备ID获取失败!");
                return;
            }
            await _autoEquipmentOperateService.CreatBatchAutoEquipmentNoResultAsync(ids, RecordCanStartEquipmentAsync, false);
            await _rcsDomainService.InitRcsInfoAsync();
            await CustomInitAsync();
        }

        #region virtual
        protected virtual async Task CustomInitAsync()
        { 
            
        }
        #endregion

        #region private
        /// <summary>
        /// 记录可连接的自动化设备
        /// </summary>
        /// <param name="eqInfos"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private async Task RecordCanStartEquipmentAsync(AutoEquipmentDto eqInfo)
        {
            //var eqInfo = eqInfos.FirstOrDefault(e => e.id == id);
            if (eqInfo is null) return;
            var service = GetAutoEquipmentService(eqInfo.equipmentType);
            if (service is not null)
            {
               await service.AddConEquipmentAsync(eqInfo.id, eqInfo, _rcsDomainService.GetRcsService);
            }
        }
        /// <summary>
        /// 根据设备类型获取自动化设备服务
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private IAutoEquipmentDomainService GetAutoEquipmentService(string type)
        {
            IAutoEquipmentDomainService methond = null;
            switch (type)
            {
                case ConstStringData.Optical_eq:
                    methond = _opticalDomainService;
                    break;
                case ConstStringData.AGV_eq:
                    methond = _agvDomainService;
                    break;
                case ConstStringData.Pallet_eq:
                    methond = null;
                    break;
                case ConstStringData.Line_eq:
                    methond = null;
                    break;
                case ConstStringData.Lift_eq:
                    methond = null;
                    break;
                case ConstStringData.Door_eq:
                    methond = null;
                    break;
                case ConstStringData.Rotate_eq:
                    methond = null;
                    break;
                case ConstStringData.AirShower_eq:
                    methond = null;
                    break;
                case ConstStringData.Cargo_eq:
                    methond = null;
                    break;
                case ConstStringData.Detector_eq:
                    methond = null;
                    break;
            }
            return methond;
        }

        public virtual async Task Test()
        {
            
        }
        public virtual async Task ComonTest()
        { 
            
        }
        #endregion

        #region 获取指定服务
        public ILineDomainService QueryLineService()
        { 
            return _lineDomainService;
        }
        public IOpticalDomainService QueryOpticalService()
        {
            return _opticalDomainService;
        }
        public IAGVDomainService QueryAGVService()
        {
            return _agvDomainService;
        }
        #endregion
    }
}
